package com.pks.autoInsurance.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pks.autoInsurance.dto.RegistrationDto;
import com.pks.autoInsurance.helper.AdminHelper;
import com.pks.autoInsurance.helper.LoginHelper;
import com.pks.autoInsurance.helper.UserLevels;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doPost::::begin");
		String ssn = request.getParameter("ssn");
		String passwd = request.getParameter("passwd");
		System.out.println("user:::" + ssn);
		System.out.println("passwd::::" + passwd);
		RegistrationDto loginDto = new RegistrationDto();
		loginDto.setSsn(ssn);
		loginDto.setPasswd(passwd);
		System.out.println("::::loginDto::::" + loginDto);
		int authResult = new LoginHelper().authenticateUser(loginDto);
		System.out.println("::::authResult==" + authResult);

		// For Normal Valid User
		if (authResult == UserLevels.VALID_USER) {
			HttpSession session = request.getSession(false);
			session.setAttribute("ssn", ssn);
			try {
				response.sendRedirect("home.jsp");
			} catch (IOException e) {
				System.err.println(getClass() + "::::" + e.getMessage());
			}

		} else if (authResult == UserLevels.VALID_ADMIN) {
			// For Admin user
			HttpSession session = request.getSession(false);
			session.setAttribute("ssn", ssn);

			// Retrive list of all the users
			request.setAttribute("registrationDtos",
					new AdminHelper().getUsersList());
			request.getRequestDispatcher("adminManage.jsp").forward(request,
					response);
			return;
		} else {
			// Display Error Message to the user for incorrect login credentials
			request.setAttribute("error",
					"The username or password you entered is incorrect.");
			request.getRequestDispatcher("/").forward(request, response);
		}
		System.out.println(getClass() + "::::doPost::::end");
	}

	/*
	 * Perform the Logout Action of the User
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doGet::::begin");
		if ("logout".equals(request.getParameter("task"))) {
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("");
		}
		System.out.println(getClass() + "::::doGet::::end");
	}

}
